package com.service.Impl;

import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mapper.TopPickTicketMapper;
import com.pojo.dto.Message;
import com.pojo.entity.CTopPick;
import com.pojo.entity.CTopPickTicket;
import com.pojo.vo.CUsedVo;
import com.pojo.vo.SelectVo;
import com.pojo.vo.TopPickTicketVo;
import com.pojo.vo.TopPickVo;
import com.service.SysUtilService;
import com.service.TopPickTicketService;
import com.util.PojoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;

@Service("TopPickTicketService")
public class TopPickTicketServiceImpl implements TopPickTicketService {
    @Autowired
    TopPickTicketMapper topPickTicketMapper;
    @Autowired
    SysUtilService sysUtilService;
    @Autowired
    Snowflake snowflake;

    @Override
    public Message<List<TopPickTicketVo>> list(TopPickTicketVo topPickTicketVo) {
        QueryWrapper<CTopPickTicket> wrapper = new QueryWrapper<>();
        List<CTopPickTicket> cTopPickTickets = topPickTicketMapper.selectList(wrapper);
        List<TopPickTicketVo> message = cTopPickTickets.stream().map(c -> {
                    TopPickTicketVo topPickTicketVo1 = PojoUtil.copyObject(c, TopPickTicketVo.class);
                    SelectVo type_name = sysUtilService.selectone(topPickTicketVo1.getPickType(), "c_top_type");
                    topPickTicketVo1.setPickName(type_name.getText());
                    return topPickTicketVo1;
                }
        ).collect(Collectors.toList());
        return Message.successMessage((long) cTopPickTickets.size(), message);
    }

    @Override
    public Message<TopPickTicketVo> get(TopPickTicketVo topPickTicketVo) {
        QueryWrapper<CTopPickTicket> wrapper = new QueryWrapper<>();
        wrapper.eq("id", topPickTicketVo.getId());
        CTopPickTicket cTopPickTicket = topPickTicketMapper.selectOne(wrapper);
        return Message.successMessage(1L, PojoUtil.copyObject(cTopPickTicket, TopPickTicketVo.class));
    }

    @Override
    public Message<TopPickTicketVo> edit(TopPickTicketVo topPickTicketVo) {
        CTopPickTicket cTopPickTicket = PojoUtil.copyObject(topPickTicketVo, CTopPickTicket.class);
        int i = topPickTicketMapper.updateById(cTopPickTicket);//根据ID更新book表
        if (i == 0) return Message.errorMessage("1001", "更新失败");
        else return Message.successMessage(1L, topPickTicketVo);
    }

    @Override
    public Message<TopPickTicketVo> add(TopPickTicketVo topPickTicketVo) {
        CTopPickTicket cTopPickTicket = PojoUtil.copyObject(topPickTicketVo, CTopPickTicket.class);
        cTopPickTicket.setChargeTime(new Date());
        cTopPickTicket.setTicket(snowflake.nextId());
        int i = topPickTicketMapper.insert(cTopPickTicket);
        if (i == 0) return Message.errorMessage("1001", "添加失败");
        else return Message.successMessage(1L, topPickTicketVo);
    }

    @Override
    public Message<TopPickTicketVo> delete(TopPickTicketVo topPickTicketVo) {
        int i = topPickTicketMapper.deleteById(topPickTicketVo.getId());
        if (i == 0) return Message.errorMessage("1001", "删除失败");
        else return Message.successMessage(1L, null);
    }

    @Override
    public Long count(Long batchNum) {
        QueryWrapper<CTopPickTicket> wrapper = new QueryWrapper<>();
        wrapper.eq("batch_num", batchNum);
        Long integer = Long.valueOf(topPickTicketMapper.selectCount(wrapper));
        return integer;


    }

    @Override
    public void open(TopPickVo topPickVo) {
        QueryWrapper<CTopPickTicket> wrapper = new QueryWrapper<>();
        wrapper.eq("batch_num", topPickVo.getBatchNum());
        List<CTopPickTicket> cTopPickTickets = topPickTicketMapper.selectList(wrapper);
        Random random = new Random();
        Integer l = random.nextInt(cTopPickTickets.size() + 1);

        cTopPickTickets.stream().map(c -> {
            c.setTransportNum(cTopPickTickets.get(l).getTicket());
            int i = topPickTicketMapper.updateById(cTopPickTickets.get(l));
            return c;
        }).collect(Collectors.toList());

    }
}

